package club.itstudio.IContainer.annotation;

import club.itstudio.IContainer.entity.User;
import club.itstudio.IContainer.exception.PermissionException;
import club.itstudio.IContainer.service.AuthService;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;


@Aspect
@Component
public class TeacherRequiredHandler {
    private AuthService authService;

    @Around("@annotation(club.itstudio.IContainer.annotation.TeacherRequired)")
    public Object teacherRequired(ProceedingJoinPoint joinPoint) throws Throwable {
        User user = authService.threadLocalUsers.get();
        if (user == null || user.userType != 1) throw new PermissionException();
        return joinPoint.proceed();
    }

    @Autowired
    public void setAuthService(AuthService authService) {
        this.authService = authService;
    }
}
